package com.scalar.chiptrack.operations.finishedGoods;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.scalar.ScalarException;
import com.scalar.chiptrack.commons.ErrorMessages;
import com.scalar.chiptrack.operations.finishedGoods.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsEntryForm;
import com.scalar.chiptrack.operations.process.VendorLocation;

/**
 * User: Murali Krishna Yengala
 * Date: May 30, 2005
 * Time: 4:04:24 PM
 */
public class FinishedGoodsHelper implements ErrorMessages{

    public static void setBaseProductsAndCustomers( HttpServletRequest request, HashMap errors)
    {
        ArrayList baseProductsList = null;
        ArrayList customersList = null;
        try
        {
            baseProductsList = FinishedGoodsDAO.getBaseProducts();
        }
        catch( ScalarException e)
        {
            errors.put( "Couldn't fetch base products list from Database !", null);
        }
        try
        {
            customersList = FinishedGoodsDAO.getAllCustomers();
        }
        catch( ScalarException e)
        {
            errors.put( "Couldn't fetch customers list from Database !", null);
        }
        request.setAttribute("baseProductsList", baseProductsList);
        request.setAttribute("customersList", customersList);
    }
    public static void setCustomers( HttpServletRequest request, HashMap errors)
    {
        ArrayList customersList = null;
        try
        {
            customersList = FinishedGoodsDAO.getAllCustomers();
        }
        catch( ScalarException e)
        {
            errors.put( "Couldn't fetch customers list from Database !", null);
        }
        request.setAttribute("customersList", customersList);
    }

    public static ArrayList getVendorLocations ( HttpServletRequest request )
    {
        ArrayList vendorLocations = new ArrayList ();
        String[] locationName = request.getParameterValues ( "locationName" );
        if ( locationName.length == 0 )
        {
            return null;
        }
        String[] priority 	= request.getParameterValues ( "priority" );
        String[] locationID = request.getParameterValues ( "locationID" );
		String select[] = request.getParameterValues ( "select" );
        VendorLocation location = null;
        int selectCount = 0;

        for ( int i = 0 ; i < locationName.length ; i++ )
        {
            try
            {
                if ( priority[ i ].trim ().length () > 0 )
                {    // check if this row was enabled by the user or not
                    // save only if its enabled
                    location = new VendorLocation ( select[ selectCount ] , priority[ i ] ,
                            locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                    selectCount++;
                }
                else
                {
                    location = new VendorLocation ( "" , "" , locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                }
            }
            catch ( Exception ignore )
            {
            }
        }
        return vendorLocations;
    }

    public static ArrayList getVendorLocations ( HttpServletRequest request , boolean bool )
    {
        ArrayList vendorLocations = new ArrayList ();
        String[] locationName = request.getParameterValues ( "locationName" );

        if ( locationName.length == 0 )
        {
            return null;
        }
        String[] priority = request.getParameterValues ( "priority" );
        String[] locationID = request.getParameterValues ( "locationID" );
        VendorLocation location = null;
        String select[] = request.getParameterValues ( "select" );
        int selectCount = 0;
        for ( int i = 0 ; i < locationName.length ; i++ )
        {

            try
            {
                if ( priority[ i ].trim ().length () > 0 )
                {    // check if this row was enabled by the user or not
                    // save only if its enabled
                    location = new VendorLocation ( select[ selectCount ] , priority[ i ] ,
                            locationName[ i ] , locationID[ i ] );
                    vendorLocations.add ( location );
                    selectCount++;
                }
            }
            catch ( Exception ignore )
            {
                ignore.printStackTrace();
            }
        }
        return vendorLocations;
    }

    public static void resetFinishedGoodsEntryForm( FinishedGoodsEntryForm form)
    {
        if( form != null)
        {
            form.setFinishedGoodsPartNo( null);
            form.setFinishedGoodsProductId( -1);
            form.setMktgPartNo( null);
            form.setProgramCode( null);
            form.setDescription( null);
            form.setCrc( "0");
            form.setAlgorithm( "" );
            form.setEquivalentPartNosList( null);
            form.setParametersList( null);
            form.setVendorList( null);
            form.setIsBlankProduct( null );
        }
    }

    public static void setFgiProducts( int baseProductIdI,HttpServletRequest request, HashMap errors, HashMap info)
    {
        if ( baseProductIdI != -1)
        {
            try
            {
                ArrayList fgiProductsList = null;
                fgiProductsList = FinishedGoodsDAO.getFgiProducts( baseProductIdI);
                if( fgiProductsList == null || fgiProductsList.isEmpty())
                {
                    info.put( "Couldn't find FGI Products for this Base Product ", null);
                }
                else
                {
                    request.setAttribute("fgiProductsList", fgiProductsList);
                }
            }
            catch( ScalarException e)
            {
                e.printStackTrace();
                errors.put( ERROR_INTERNAL_SERVER_ERROR, "");
            }
        }
    }
}
